package com.javaweb.aop;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

/**
 * @desc:
 * @author: Ping
 * @date: 2024/10/16 16:12
 */
@Component
@Aspect
@Slf4j
public class CatchExceptionAspect {

    @Pointcut("@within(com.javaweb.annotation.CatchException)")
    public void allCatchExceptionAnnotation(){}

    @Around("allCatchExceptionAnnotation()")
    public Object tryCatchException(ProceedingJoinPoint pjp) {

        log.info("INFO from CatchExeptionAnnotation-Aspect for method {}",
                pjp.getSignature().getName()) ;

        Object ret = null ;
        try {
            // BEFORE join-point
            // EXACTLY that join point:  userDao.selectOne()
            ret = pjp.proceed();
            // AFTER join-point
            log.info("INFO from CatchExeptionAnnotation-Aspect for method {}: with return {}",
                    pjp.getSignature().getName(), ret) ;
            return ret ;
        } catch (Throwable e) {
            log.error("INFO from CatchExeptionAnnotation-Aspect for method {}: {}",
                    pjp.getSignature().getName(), e.getMessage()) ;
        }
        return null ;


    }





}
